package layer
{
	import map.data.Obstacle;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.utils.Dictionary;
	
	import view.ObstacleView;

	public class ObstacleLayer extends BaseLayer
	{
		private var _obstacles:Vector.<ObstacleView>;
		private var _currentSelected:ObstacleView;
		public function ObstacleLayer()
		{
			super();
			_obstacles = new Vector.<ObstacleView>();
		}
		
		public function addObstacle(ob:Obstacle):void
		{
			if(ob.length > 1)
			{
				for each(var o:ObstacleView in _obstacles)
				{
					if(o.obstacle.eqaul(ob))
					{
						return;
					}
				}
				var obv:ObstacleView = new ObstacleView(ob);
				addChild(obv);
				obv.addEventListener(MouseEvent.CLICK,onMouseClick);
			}
			_obstacles.push(obv);
		}
		
		public function onMouseClick(event:MouseEvent):void
		{
			if(_optionType == MapEditor.SELECT)
			{
				for each(var obv:ObstacleView in _obstacles)
				{
					obv.selected = false;
					obv.filters = [];
				}
				obv = event.currentTarget as ObstacleView;
				obv.selected = true;
				_currentSelected = obv;
			}
		}
		
		override public function getData():*
		{
			var result:Vector.<Obstacle> = new Vector.<Obstacle>();
			for each(var obs:ObstacleView in _obstacles)
			{
				result.push(obs.obstacle);
			}
			return result;
		}
		
		override public function deleteCurrentSelected():void
		{
			if(_optionType == MapEditor.SELECT)
			{
				if(_currentSelected)
				{
					removeChild(_currentSelected);
					_obstacles.splice(_obstacles.indexOf(_currentSelected),1);
				}
				_currentSelected = null;
			}
		}
	}
}